package top.meeting.room.module.book.corn;

import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.SqlPara;
import com.xiaoleilu.hutool.date.DateTime;
import com.xiaoleilu.hutool.date.DateUtil;
import net.dreamlu.event.EventKit;
import top.meeting.room.module.book.event.ApproveEvent;
import top.meeting.room.module.book.event.BookMeetEvent;
import top.meeting.room.module.book.model.Book;

import java.util.Date;
import java.util.List;

/**
 * Created by yunqisong on 2017/10/17.
 */
public class InformOpenDoor implements Runnable {

    @Override
    public void run() {
        System.out.println("time:" + new DateTime());
        Date now = new Date();
        String today = DateUtil.format(now, "yyyy-MM-dd");
        Long bookDate = DateUtil.parseDate(today).getTime();
        SqlPara sqlPara = Book.dao.getSqlPara(Book.book_pageModel, Kv.by("bookDate", bookDate).set("status", 1));
        sqlPara.setSql(sqlPara.getSql().replaceFirst(" AND ", " WHERE "));
        List<Book> books = Book.dao.find(sqlPara);
        long minMills = 1000 * 60;
        books.forEach(book -> {
            Date startTime = book.getStartTime();
            if (now.getTime() <= startTime.getTime()) {
                if (DateUtil.betweenMs(startTime, now) / minMills <= 10) {
                    //发送一个提醒开门事件
                    EventKit.post(new BookMeetEvent(book, "提醒开门"));
                    //发送一个执行会议事件
                    EventKit.post(new ApproveEvent(book, "执行会议"));
                }
            }
        });

    }

    public static void main(String[] args) {
        DateTime parse = DateUtil.parse("2017-10-17 14:10:10");
        DateTime parse1 = DateUtil.parse("2017-10-17 13:21:20");
        long l = DateUtil.betweenMs(parse, parse1);
        long minMills = 1000 * 60;
        System.out.println(l / minMills);
    }
}
